<?php

namespace App\Http\Controllers\Api;

use App\Libs\Message;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use Config , Log , Validator , Cache;

use App\Libs\Sms;

class SendSmsController extends Controller
{

    public function checkCode(Request $request){
        $validation = Validator::make($request->all() , [
            'mobile'                  => 'required',
        ] , [
            'mobile.required'               => '没有填写手机号',
        ]);
        if($validation->fails()){
            $error = $validation->errors()->all();
            return response()->json(Message::setResponseInfo('FAILED', '', '402', $error[0]));
        }

        $mobile = str_replace(' ', '', trim($request->get('mobile')));

        if(!isMobileNum($mobile)){
            return response()->json(Message::setResponseInfo('FAILED', '', '401', '不支持该号码'));
        }

        $code = getSalt('6' , 1);

        $time = 1;

        if($mobile == '18874130126'
        ){
            $code = '123456';
        }
        Cache::put($mobile , $code , $time);

        Log::debug('验证码:'.$code);

        if($mobile != '18874130126') {
            $smsLib = new Sms(Config::get('sms.apikey'), Config::get('sms.apisecret'));
            $res = $smsLib->send($mobile, "【广西电信】验证码{$code},有效期{$time}分钟。正在进行手机绑定，请勿将验证码泄露于他人。");
        }else{
            $res = true;
        }

        if($res === true) {
            return response()->json(Message::setResponseInfo('SUCCESS'));
        }else{
            return response()->json(Message::setResponseInfo('FAILED'));
        }
    }
}
